/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-7-9
 */
package com.flyee.utils{
	
	import com.flyee.service.pool.ObjectCollection;
	
	import flash.geom.Point;	
	
	/**
	 * 斜坐标系与垂直坐标系转换工具类
	 */
	public class CoodinateUtil{
		
		public function CoodinateUtil(){
			
		}
		
		public static const MIN_WIDTH  : int  = 80;
		public static const MIN_HEIGHT : int = 40;
		
		/**
		 * 斜坐标转换直角坐标
		 */
		public static function convertToVerticalXY(oPoint : Point, w : int = MIN_WIDTH/2, h :int = MIN_HEIGHT/2) : Point {
			var vPoint : Point = new Point();
			vPoint.x = oPoint.x * w - oPoint.y * w;
			vPoint.y = (oPoint.x + oPoint.y) * h;
			return vPoint;
		}
		
		/**
		 * 直角坐标转换斜坐标
		 */	
		public static function convertToObliqueXY(vPoint : Point, w : int = MIN_WIDTH/2, h :int = MIN_HEIGHT/2) : Point {
			var oPoint : Point = new Point();
			oPoint.y = Math.floor((vPoint.y - vPoint.x / 2) / w);
			oPoint.x = Math.floor((vPoint.y + vPoint.x / 2) / w);
			return oPoint;
		}
		
		/**
		 * 判断矢量方向（8方向）
		 */
		public static function calculateDirection(dx : Number, dy : Number) : Number {
			if(dx==0&&dy==0) return 3;
			var direct : int;
			direct = Math.round((Math.atan2(dy, dx))/(Math.PI/4));
			direct = direct > 5 ? direct - 6 : direct + 2;
			if(direct==-2) direct = 6;				
			return direct; 
		}	
		
		/**
		 * 判断距离 
		 */
		public static function checkDistance(srcX : Number, srcY : Number, tarX : Number, tarY : Number) : Number {		
			var dx : Number;
			var dy : Number;
//			if(target == null){
//				dx = Math.abs(ObjectCollection.master.x - ObjectCollection.master.target.x);
//				dy = Math.abs(ObjectCollection.master.y - ObjectCollection.master.target.y);
//			}else{
			dx = Math.abs(srcX - tarX);
			dy = Math.abs(srcY - tarY);
//			}
			return (dx > dy) ? dx : dy; 
		}

	}
	
}